package com.liuym.admin.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyun.oss.OSSClient;
import com.liuym.admin.properties.OssProperties;
import com.liuym.admin.repository.mapper.AdminMapper;
import com.liuym.admin.repository.model.bo.AdminBO;
import com.liuym.admin.repository.model.entity.Admin;
import com.liuym.admin.service.AdminService;
import com.liuym.common.constants.RedisConstants;
import com.liuym.common.dubbo.classes.api.ClassesDubboService;
import com.liuym.common.dubbo.student.api.StudentDubboService;
import com.liuym.common.dubbo.student.api.StudentScoreDubboService;
import com.liuym.common.exception.AdminException;
import com.vip.vjtools.vjkit.mapper.BeanMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author LiuYiMing
 * @date 2018/10/25 17:21
 * @describe：
 */
@Service
public class AdminServiceImpl implements AdminService {

    private static Logger logger = LoggerFactory.getLogger(AdminServiceImpl.class);

    @Reference(version = "${student.dubbo.version}")
    private StudentDubboService studentDubboService;

    @Reference(version = "${student.dubbo.version}")
    private StudentScoreDubboService studentScoreDubboService;

    @Reference(version = "${class.dubbo.version}")
    private ClassesDubboService classesDubboService;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private OssProperties ossProperties;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public Long getStudentNumberById(int id) {
        return null;
    }

    @Override
    public AdminBO getAdminByUserName(String userName) throws AdminException {
        Admin admin = adminMapper.getAdminByUserName(userName);
        if (admin != null) {
            AdminBO adminBO = BeanMapper.map(admin, AdminBO.class);
            return adminBO;
        }
        return null;
    }

    @Override
    public List<AdminBO> getAllAdmin() {
        List<Admin> allAdminList = adminMapper.getAllAdmin();
        if (allAdminList.size() > 0 && allAdminList != null) {
            // jdk1.8实现list类型转换
            List<AdminBO> allAdminBOList =
                    allAdminList
                            .stream()
                            .map(s -> BeanMapper.map(s, AdminBO.class))
                            .collect(Collectors.toList());
            return allAdminBOList;
        }
        return null;
    }

    @Override
    public void updateAdmin(AdminBO adminBO) {
        Admin admin = null;
        if (adminBO != null) {
            admin = BeanMapper.map(adminBO, Admin.class);
        }
        adminMapper.updateByPrimaryKeySelective(admin);
    }

    @Override
    public void deleteAdminById(int id) throws AdminException {
        adminMapper.deleteByPrimaryKey(id);
    }


    @Override
    public void addAdmin(AdminBO adminBO) {
        Admin admin = null;
        if (adminBO != null) {
            admin = BeanMapper.map(adminBO, Admin.class);
        }
        adminMapper.insertSelective(admin);
    }

    @Override
    public AdminBO getAdminById(int id) {
        Admin admin = adminMapper.selectByPrimaryKey(id);
        if (admin != null) {
            return BeanMapper.map(admin, AdminBO.class);
        }
        return null;
    }

    @Override
    public String download(String path, String fileName) {
        OSSClient ossClient = new OSSClient(ossProperties.endpoint,
                ossProperties.accessKeyId, ossProperties.accessKeySecret);
        Date expiration = new Date(new Date().getTime() + 3600 * 1000 * 24 * 365 * 10);
        URL url = ossClient.generatePresignedUrl(ossProperties.bucketName, path, expiration);
        ossClient.shutdown();
        return url.toString();
    }

    @Override
    public void logout(String token) {
        redisTemplate.delete(RedisConstants.ADMIN_LOGIN_USER_PREFIX + token);
    }

}
